#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
          
#include <AL/al.h>
#include <AL/alc.h>
#include <AL/alext.h>
          
int
main (int argc, char **argv)
{
       ALCdevice *dev;
       void      *id_contexto = NULL;
       void      *wave        = NULL;

       ALfloat ceros[]    = {0.0f, 0.0f,  0.0f};
       ALfloat atras[]    = {0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f};
       ALfloat position[] = {2.0f, 0.0f, -4.0f};

       ALuint    boom;
       ALuint    multis;
       ALsizei   tamano;
       ALsizei   bits;
       ALsizei   frec;
       ALsizei   formato;
       ALboolean err;
       ALint     estado;


       dev = alcOpenDevice (NULL);
       if (dev == NULL) {
    	          fprintf (stderr, "Error al abrir dispositivo\n");
               return 1;
       }

       id_contexto = alcCreateContext (dev, NULL);
       if (id_contexto == NULL) {
  	          fprintf (stderr, "Error al crear el contexto\n");
                        alcCloseDevice (dev);
                return 1;
        }

        alcMakeContextCurrent (id_contexto);
         
        alListenerfv (AL_POSITION, ceros);
        alListenerfv (AL_VELOCITY, ceros);

        alGenBuffers (1, &boom);

        err = alutLoadWAV ("ejemplo.wav", &wave, &formato, 
                           &tamano, &bits, &frec);
        if(err == AL_FALSE) {
               fprintf (stderr, "Imposible abrir el ejemplo.wav\n");
               exit(1);
        }

        alBufferData (boom, formato, wave, tamano, frec);
        free (wave);

	
        alGenSources (1, &multis);
	
        alSourcefv (multis, AL_POSITION, position);
        alSourcefv (multis, AL_VELOCITY, ceros);
        alSourcefv (multis, AL_ORIENTATION, atras);
                   
        alSourcei (multis, AL_LOOPING, AL_FALSE);
        alSourcef (multis, AL_GAIN_LINEAR_LOKI, 1.0);
	
        alQueuei (multis, AL_BUFFER, boom);

        alSourcePlay (multis);

        alGetSourceiv(multis, AL_SOURCE_STATE, &estado);
        while (estado == AL_PLAYING) {
                usleep (1000);
                alGetSourceiv(multis, AL_SOURCE_STATE, &estado);
        };

        alcMakeContextCurrent (NULL);
        alcDestroyContext (id_contexto);
          
        alcCloseDevice (dev);
          
        return 0;
}
